package com.example.demo.information.service.impl;

import com.example.demo.common.utils.Constant;
import com.example.demo.information.entity.Historyphoto;
import com.example.demo.information.entity.Photo;
import com.example.demo.information.entity.Student;
import com.example.demo.information.entity.info.StudentInfo;
import com.example.demo.information.repository.HistoryphotoRepository;
import com.example.demo.information.service.HistoryPhotoService;
import com.example.demo.information.service.PhotoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class HistoryPhotoServiceImpl implements HistoryPhotoService {
    @Autowired
    private HistoryphotoRepository historyphotoRepository;

    /**
     * 保存历史记录photo ByStudent
     * @param student
     * @param spId
     * @param bkColor
     * @param originalPath
     * @param resultSavePath
     * @param internetPath
     * @param xgr
     * @param xgsj
     * @return
     */
    @Override
    public String saveTempPhotoByStudent(Student student, String spId, String bkColor
            , String originalPath, String resultSavePath, String internetPath, String xgr,String xgsj) {
        Historyphoto tempPhoto = new Historyphoto(student);
        tempPhoto.setLrr(xgr);
        tempPhoto.setLrsj(xgsj);
        tempPhoto.setYtpath(originalPath);
        tempPhoto.setApipath(internetPath);
        tempPhoto.setSavepath(resultSavePath);
        tempPhoto.setSpid(spId);
        tempPhoto.setBkcolor(bkColor);
        historyphotoRepository.save(tempPhoto);
        return "success";
    }

    /**
     * 保存历史记录photo byStudentInfo
     * @param studentInfo
     * @param spId
     * @param bkColor
     * @param originalPath
     * @param resultSavePath
     * @param internetPath
     * @param xgr
     * @param xgsj
     * @return
     */
    @Override
    public String saveTempPhotoByStudentInfo(StudentInfo studentInfo, String spId, String bkColor
            , String originalPath, String resultSavePath, String internetPath, String xgr, String xgsj) {
        Historyphoto tempPhoto = new Historyphoto(studentInfo);
        tempPhoto.setLrr(xgr);
        tempPhoto.setLrsj(xgsj);
        tempPhoto.setYtpath(originalPath);
        tempPhoto.setApipath(internetPath);
        tempPhoto.setSavepath(resultSavePath);
        tempPhoto.setSpid(spId);
        tempPhoto.setBkcolor(bkColor);
        historyphotoRepository.save(tempPhoto);
        return "success";
    }

    /**
     * 更新historyPhoto内的数据（用于原图已上传，将原图转化成证件照后，historyPhoto对象要更新）
     * @param tempPhoto
     * @param spId
     * @param bkColor
     * @param resultSaveObjectName
     * @param internetPath
     * @param xgr
     * @param xgsj
     * @return
     */
    @Override
    public String updateTempPhotoByResultInfo(Historyphoto tempPhoto, String spId, String bkColor
            , String resultSaveObjectName, String internetPath, String xgr, String xgsj) {
        if(StringUtils.isBlank(tempPhoto.getLrr())&&StringUtils.isBlank(tempPhoto.getLrsj())){
            tempPhoto.setLrr(xgr);
            tempPhoto.setLrsj(xgsj);
        }else{
            tempPhoto.setXgr(xgr);
            tempPhoto.setXgsj(xgsj);
        }
        tempPhoto.setApipath(internetPath);
        tempPhoto.setSavepath(resultSaveObjectName);
        tempPhoto.setSpid(spId);
        tempPhoto.setBkcolor(bkColor);
        historyphotoRepository.save(tempPhoto);
        return Constant.SUCCESS;
    }


    @Override
    public Historyphoto findByXsxxIdAndXxxxIdAndBjxxId(Long xsxxId, Long xxxxId, Long bjxxId) {
        List<Historyphoto> list = historyphotoRepository.findByXsxxIdAndXxxxIdAndBjxxIdOrderByLrsjDesc(xsxxId,xxxxId,bjxxId);
        return list!=null&&!list.isEmpty()?list.get(0):null;
    }

    @Override
    public Historyphoto findById(Long historyPhotoId) {
        return historyphotoRepository.findById(historyPhotoId).get();
    }


}
